<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
				</div>
				<div class="flex-end-center-wrap">
					<div class="search-bar-item-size2 item-gap">
						<el-date-picker v-model="searchParams.yearMonth" :clearable="false" size="small" style="width: 100%;" type="month" value-format="yyyy-MM" placeholder="月" />
					</div>
					<el-select v-model="searchParams.typeCode" placeholder="检验类型" size="small" clearable class="search-bar-item-size1 margin-5-0 item-gap" @change="onWorkshopChange">
						<el-option
							v-for="item in examineList"
							:key="item.code"
							:label="item.name"
							:value="item.code"
						>
						</el-option>
					</el-select>
					<jk-search-input v-model="searchParams.productNameOrCode" placeholder="品种编码或名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>
		<jk-card>
			<jk-table ref="jkTable" :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<div
					v-for="(item, index) in tableHeader"
					:key="index"
				>
					<vxe-colgroup
						v-if="typeof item.key !== 'string'"
						:title="item.value"
						:field="typeof item.key !== 'string' ? 'aaa': item.key"
						:align="['belongDate','productName'].includes(item.key) ? 'center': 'center'"
						width="180"
					>
						<vxe-column
							v-for="(childItem, childIndex) in item.key"
							:key="childIndex"
							:field="childItem.key"
							:title="childItem.value"
							align="right"
							width="110"
						>
						</vxe-column>
					</vxe-colgroup>
					<vxe-column
						v-else
						:title="item.value"
						:fixed="item.fixed ? item.fixed : ''"
						:field="typeof item.key !== 'string' ? 'aaa': item.key"
						:align="['belongDate','productName'].includes(item.key) ? item.align: 'center'"
						width="180"
					></vxe-column>
				</div>
			</jk-table>
		</jk-card>
	</div>
</template>

<script>
    import { auditStateList } from '@/utils/jk/status-option-list';
    import calHeight from '/src/mixins/cal-table-height';
    import { formatMonth, pageConfig } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import handlerCache from '../../../mixins/handler-cache';
    import { spinQualityInspectionRecordMonthData } from '../../../api/quality/qualityDayReportForms';
    import { systemBusinessDictPage } from '../../../api/quality/processQuality';
    export default {
        name: 'WorkshopArea',
        components: {
        },
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                apiItem: {
                    'page': spinQualityInspectionRecordMonthData
                },
                importDialogState: false,
                auditStateList: auditStateList,
                detailDialogState: false,
                dataId: null,
                saveDialogState: false,
                tableLoading: true,
                exportLoading: false,
                tableHeader: [],
                tableData: [],
                examineList: [],
                searchParams: {
                    yearMonth: '',
                    typeCode: '',
                    productNameOrCode: ''
                }
            };
        },
        methods: {
            getTypeListRequest() {
                const params = {
                    categoryCode: 'QUALITY_TYPE',
                    pageSize: 100,
                    pageNo: 1
                };
                return systemBusinessDictPage(params).then(res => {
                    if (res.data.status === 200) {
                        this.examineList = res.data.res || [];
                        if (!this.searchParams.typeCode) {
                            this.$set(this.searchParams, 'typeName', res.data.res[0].name);
                            this.$set(this.searchParams, 'typeCode', res.data.res[0].code);
                            this.$set(this.searchParams, 'typeId', res.data.res[0].id);
                        }
                    }
                });
            },
            onWorkshopChange(e) {
                this.examineList.forEach(x => {
                    if (e === x.id) {
                        this.$set(this.searchParams, 'typeName', x.name);
                        this.$set(this.searchParams, 'typeCode', x.code);
                        this.$set(this.searchParams, 'typeId', x.id);
                    }
                });
            },
            onVisibleChangeOfImportDialog(e) {
                this.importDialogState = e;
            },
            onFinishBtnOfImportDialog() {
                this.importDialogState = false;
                this.getListRequest();
            },
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            onDetailBtn(e) {
                this.dataId = e.id;
                this.detailDialogState = true;
            },
            onVisibleChangeOfDetailDialog(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.detailDialogState = e;
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.saveDialogState = e;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                if (params.yearMonth) {
                    params.year = new Date(params.yearMonth).getFullYear();
                    params.month = new Date(params.yearMonth).getMonth() + 1;
                }
                delete params.typeName;
                delete params.typeId;
                this.apiItem.page(params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res.tableData;
                    }
                    if (res.data.res.tableHeader) {
                        const arr = [];
                        res.data.res.tableHeader.forEach(x => {
                            arr.push({
                                value: x.value,
                                key: x.key,
                                fixed: ['belongDate', 'productName'].includes(x.key) ? 'left' : '',
                                width: ['belongDate', 'productName'].includes(x.key) ? 80 : 120,
                                align: ['belongDate', 'productName'].includes(x.key) ? 'left' : 'right'
                            });
                            return x;
                        });
                        this.tableHeader = arr;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.getListRequest();
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.dataId = row.id;
                this.saveDialogState = true;
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            async getDependentData() {
                this.$set(this.searchParams, 'yearMonth', formatMonth(Date.now()));
                await this.getTypeListRequest();
                await this.getMenuColumnListRequest();
                this.getListRequest();
            }
        },
        mounted() {
            this.getDependentData();
        }
    };
</script>
